// 初始化axios实例
import axios from 'axios';


const server = {
  // url: 'https://c-c.jiahuagame.com/lefantian-console/console',
  // url: 'https://c-c.shuaishuaiup.com/jiahua-strategy',
  url: 'http://10.10.10.155:8198/jiahua-strategy',
  auth: 'Xyz287454jY56345ok56V5423896v55T'
}

if (window.location.href.match('shuaishuaiup')) {
  server.url = 'https://c-c.shuaishuaiup.com/jiahua-strategy';
}

if (window.location.href.match('lefantian')) {
  server.url = 'http://lefantian.jiahuagame.com/lefantian-console/console'
  if (window.location.href.match('https')) {
    server.url = 'https://lefantian.jiahuagame.com/lefantian-console/console'
  }
}

if (window.location.href.match(/\?local=1/)) {
  server.url = 'http://10.10.10.155:8080/lefantian-console/console';
}                                                                                                                                                   

window.serverObj = server
const CancelToken = axios.CancelToken;

let cancel;
const instance = axios.create({
  baseURL: server.url,
  timeout: 120000,
  cancelToken: new CancelToken(function(c) {
    cancel = c
  })
});

window.cancelToken = cancel;

// instance.defaults.headers.post['Content-Type'] = 'multipart/form-data';
instance.defaults.headers.post['Content-Type'] = 'application/json';
instance.defaults.headers['token'] = 'mingame_center';
// 错误处理
instance.interceptors.response.use(function(response) {
  return response;
}, (err) => {
  if (axios.isCancel(err)) {
    console.log('Request canceled', err.message);
  } else {
    // handle error
  }
  if (err.code === 'ECONNABORTED') {
    alert('连接超时。')
  }
  console.dir(err)
  return Promise.reject(err)
});

export default instance;